﻿using System;
using System.IO;
using System.Text;

namespace MvcCms.Service.Code
{
    //From "The Man" Rick Strahl at http://west-wind.net/Weblog/posts/574153.aspx
    public class StringUtils
    {
        /// <summary>
        /// Extracts a string from between a pair of delimiters. Only the first 
        /// instance is found.
        /// </summary>
        /// <param name="source">Input String to work on</param>
        /// <param name="StartDelim">Beginning delimiter</param>
        /// <param name="endDelim">ending delimiter</param>
        /// <param name="CaseInsensitive">Determines whether the search for delimiters is case sensitive</param>
        /// <returns>Extracted string or ""</returns>
        public static string ExtractString(string source, string beginDelim,
                                           string endDelim, bool caseSensitive,
                                           bool allowMissingEndDelimiter)
        {
            int at1, at2;

            if (string.IsNullOrEmpty(source))
                return string.Empty;

            if (caseSensitive)
            {
                at1 = source.IndexOf(beginDelim);
                if (at1 == -1)
                    return string.Empty;

                at2 = source.IndexOf(endDelim, at1 + beginDelim.Length);
            }
            else
            {
                //string Lower = source.ToLower();
                at1 = source.IndexOf(beginDelim, 0, source.Length, StringComparison.OrdinalIgnoreCase);
                if (at1 == -1)
                    return string.Empty;

                at2 = source.IndexOf(endDelim, at1 + beginDelim.Length, StringComparison.OrdinalIgnoreCase);
            }

            if (allowMissingEndDelimiter && at2 == -1)
                return source.Substring(at1 + beginDelim.Length);

            if (at1 > -1 && at2 > 1)
                return source.Substring(at1 + beginDelim.Length, at2 - at1 - beginDelim.Length);

            return string.Empty;
        }


        public static string ExtractString(string source, string beginDelim, string endDelim, bool caseSensitive)
        {
            return ExtractString(source, beginDelim, endDelim, caseSensitive, false);
        }

        public static string ExtractString(string source, string beginDelim, string endDelim)
        {
            return ExtractString(source, beginDelim, endDelim, false, false);
        }
    }
}
